package com.example.forum.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Data
@Getter
@Setter
@ApiModel("用户信息")
@NoArgsConstructor
public class User {
    private Long id;

    @ApiModelProperty("用户名")
    private String userName;

    //@JsonIgnore 表示返回前端时不参与Json序列化
    @JsonIgnore
    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户昵称")
    private String nickName;

    @ApiModelProperty("电话号码")
    private String phoneNum;

    @ApiModelProperty("邮件")
    private String email;

    @ApiModelProperty("用户性别")
    private Byte gender;

    @JsonIgnore
    @ApiModelProperty("盐值")
    private String salt;

    @JsonInclude(JsonInclude.Include.ALWAYS)//强制参与json序列化
    @ApiModelProperty("用户头像路径")
    private String avatarUrl;

    @ApiModelProperty("发帖数量")
    private Integer articleCount;

    @ApiModelProperty("是否为管理员 0否 1是")
    private Byte isAdmin;

    @ApiModelProperty("备注，自我介绍")
    private String remark;

    @ApiModelProperty("状态 0正常 1 禁言")
    private Byte state;

    @JsonIgnore
    @ApiModelProperty("是否删除 0否 1是")
    private Byte deleteState;

    @ApiModelProperty("创建时间，精确到秒")
    private Date createTime;

    @ApiModelProperty("更新时间，精确到秒")
    private Date updateTime;

    public static void setUserDefaultValues(User user) {
        if (user.getGender() == null) {
            user.setGender((byte) 2);
        }
        user.setArticleCount(0);
        user.setIsAdmin((byte) 0);
        user.setState((byte) 0);
        user.setDeleteState((byte) 0);
        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
    }

    public User(String userName, String nickName, String saltPassword, String salt) {
        this.userName = userName;
        this.password = saltPassword;
        this.nickName = nickName;
        this.salt = salt;
    }

    //校验用户密码是否为数字
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches() || str.length() != 6) {
            return false;
        }
        return true;
    }
}